package com.culturesalon.dao;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.culturesalon.connectionfactory.ConnectionFactory;
import com.culturesalon.dto.DeliverInfo;
import com.culturesalon.dto.OrderItem;
import com.culturesalon.dto.Orders;
import com.culturesalon.util.OrderState;
import com.culturesalon.util.PaymentWay;

public class OrderItemDAOImpl implements OrderItemDAO {

	@Override
	public Serializable save(OrderItem orderItem) {
		Connection conn=null;
		PreparedStatement pstm=null;
		ResultSet rs=null;
		try {
			conn=ConnectionFactory.getConnection();
			StringBuffer sql=new StringBuffer();
			sql.append(" INSERT INTO T_ORDERITEM(ID,GOODSID,AMOUNT,GOODSNAME,MARKETPRICE,ORDERID) ");
			sql.append(" VALUES(null,?,?,?,?,?) ");
			pstm=conn.prepareStatement(sql.toString(),Statement.RETURN_GENERATED_KEYS);
			pstm.setInt(1,orderItem.getGoodsid());
			pstm.setInt(2, orderItem.getAmount());
			pstm.setString(3, orderItem.getGoodsName());
			pstm.setDouble(4, orderItem.getMarketPrice());
			pstm.setString(5, orderItem.getOrderId());
			pstm.executeUpdate();
			rs=pstm.getGeneratedKeys();
			int id=0;
			if(rs.next()){
				id=rs.getInt(1);
				return id;
			}else{
				
				return null;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}finally{
			ConnectionFactory.close(conn,  rs,pstm);
		}
	}

	@Override
	public Serializable update(OrderItem orderItem) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void delete(Serializable id) {
		// TODO Auto-generated method stub

	}

	@Override
	public OrderItem get(Serializable id) {
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		StringBuffer sql = new StringBuffer();
		sql.append(" SELECT ID,GOODSID,AMOUNT,GOODSNAME,MARKETPRICE,ORDERID");
		sql.append(" FROM T_ORDERITEM ");
		sql.append(" WHERE ID=? ");
		conn = ConnectionFactory.getConnection();
		OrderItem orderItem = null;
		try {
			stmt = conn.prepareStatement(sql.toString());
			stmt.setInt(1, (Integer) id);
			rs = stmt.executeQuery();
			if (rs.next()) {
				
				orderItem=new OrderItem();
				orderItem.setId(rs.getInt("id"));
				orderItem.setGoodsid(rs.getInt("goodsid"));
				orderItem.setAmount(rs.getInt("amount"));
				orderItem.setGoodsName(rs.getString("goodsName"));
				orderItem.setMarketPrice(rs.getDouble("marketPrice"));
				orderItem.setOrderId(rs.getString("orderId"));
				
				
				
				
				
			}
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		} finally {
			ConnectionFactory.close(conn, rs, stmt);
		}
		return orderItem;
	}

	@Override
	public List<OrderItem> findAllOrderId(String orderId) {
		List<OrderItem> list = new ArrayList<OrderItem>();
		Connection conn = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		StringBuffer sql = new StringBuffer();
		sql.append(" SELECT * FROM T_ORDERITEM WHERE orderId=? ");
		conn = ConnectionFactory.getConnection();
		OrderItem orderItem = null;
		try {
			pstm = conn.prepareStatement(sql.toString());
			pstm.setString(1, orderId);
			rs = pstm.executeQuery();
			while (rs.next()) {
				orderItem=new OrderItem();
				orderItem.setId(rs.getInt("id"));
				orderItem.setGoodsid(rs.getInt("goodsid"));
				orderItem.setAmount(rs.getInt("amount"));
				orderItem.setGoodsName(rs.getString("goodsName"));
				orderItem.setMarketPrice(rs.getDouble("marketPrice"));
				orderItem.setOrderId(rs.getString("orderId"));
				list.add(orderItem);

			}
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} finally {
			ConnectionFactory.close(conn, rs, pstm);

		}
		
}
	}
